import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import net.kuakao.pub.db.DbUtils;
import net.kuakao.pub.entity.TableEntity;
import net.kuakao.pub.util.PubUtil;



public class PubMain {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		
		String projectSrcPath = "d:/test/sandbox/src";
		String projectJspPath = "d:/test/sandbox/WebRoot/WEB-INF/view";
		String entityPackages = "su";   //包名
		boolean isAdmin = true;
		String tableName = "su_log_service";  //表名
		String viewName = "logService";   //action名称
		String cnName = "service记录信息";
		
		
		TableEntity tableEntity = new TableEntity();
		tableEntity.setTableName(tableName);
		tableEntity.setViewName(viewName);
		tableEntity.setCnName(cnName);
		Map<String,Object> params = new HashMap<String,Object>();
		DbUtils.getTableColums(tableEntity);
		params.put("tableEntity", tableEntity);
		params.put("entityPackages", entityPackages);
		params.put("isAdmin", isAdmin);
		
		
		try {
			//控制器
			String controllerContent = PubUtil.process("controller.html", params);
			
			pubJavaFile(projectSrcPath, entityPackages +"."+ viewName + ".controller",  viewName.substring(0, 1).toUpperCase() + viewName.substring(1) + (isAdmin ? "Admin" : "") + "Controller.java", controllerContent);
			
			//DTO
			String dtoContent =PubUtil.process("dto.html", params);
			
			pubJavaFile(projectSrcPath, entityPackages +"."+ viewName + ".dto",  viewName.substring(0, 1).toUpperCase() + viewName.substring(1) + "Dto.java", dtoContent);
			
			//ENTITY
			String entityContent = PubUtil.process("entity.html", params);
			
			
			pubJavaFile(projectSrcPath, entityPackages +"."+ viewName + ".entity",  viewName.substring(0, 1).toUpperCase() + viewName.substring(1) + "Entity.java", entityContent);
			
			//DAO
			String idaoContent = PubUtil.process("idao.html", params);
			String daoContent = PubUtil.process("dao.html", params);
			
			pubJavaFile(projectSrcPath, entityPackages +"."+ viewName + ".dao",  "I" + viewName.substring(0, 1).toUpperCase() + viewName.substring(1) + "DAO.java", idaoContent);
			pubJavaFile(projectSrcPath, entityPackages +"."+ viewName + ".dao.impl",  viewName.substring(0, 1).toUpperCase() + viewName.substring(1) + "DAO.java", daoContent);
			
			//SERVICE
			String iserviceContent = PubUtil.process("iservice.html", params);
			String serviceContent = PubUtil.process("service.html", params);
			
			pubJavaFile(projectSrcPath, entityPackages +"."+ viewName + ".service",  "I" + viewName.substring(0, 1).toUpperCase() + viewName.substring(1) + "Service.java", iserviceContent);
			pubJavaFile(projectSrcPath, entityPackages +"."+ viewName + ".service.impl",  viewName.substring(0, 1).toUpperCase() + viewName.substring(1) + "Service.java", serviceContent);
			
			//前台显示
			String indexContent = PubUtil.process("index.html", params);
			String editContent = PubUtil.process("edit.html", params);
			String showContent = PubUtil.process("show.html", params);
			
			pubJspFile(projectJspPath, viewName,  viewName + "_index.jsp", indexContent);
			pubJspFile(projectJspPath, viewName,  viewName + "_edit.jsp", editContent);
			pubJspFile(projectJspPath, viewName,  viewName + "_show.jsp", showContent);
			
			
			System.out.println("pub Sucess!");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void pubJspFile(String projectSrcPath, String pack, String fileName, String content) {
		projectSrcPath = projectSrcPath.endsWith("/") ? projectSrcPath : (projectSrcPath+ "/");
		String fullPath = projectSrcPath + pack.replaceAll("\\.", "/");
		File f = new File(fullPath);
		if(!f.exists()) {
			f.mkdirs();
		}
		//System.out.println(content);
		writeFile(fullPath, fileName, content);
	}
	
	
	public static void pubJavaFile(String projectSrcPath, String pack, String fileName, String content) {
		projectSrcPath = projectSrcPath.endsWith("/") ? projectSrcPath : (projectSrcPath+ "/");
		String fullPath = projectSrcPath + pack.replaceAll("\\.", "/");
		File f = new File(fullPath);
		if(!f.exists()) {
			f.mkdirs();
		}
		//System.out.println(content);
		writeFile(fullPath, fileName, content);
	}
	
	public static void writeFile(String fullPath, String fileName, String content) {
		File file = new File(fullPath +"/" + fileName);
		PrintWriter pw = null;
		try {
			pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file),"utf-8"));
			 pw.println(content);   
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			if(pw != null)
				pw.close();   
		} 
	}

}
